<?php

namespace App\Http\Controllers;

use App\Models\Banner;
use Illuminate\Http\Request;

class BannerController extends Controller
{

    public function __construct(Request $request)
    {
        $user = $request->user();

        if ($user->type != 4 && $user->type != 3  && $user->type != 5) {
            throw new \Exception('权限不足');
        }

        parent::__construct();
    }

    public function index(Request $request)
    {
        $perPage = $request->get('perPage', 10);
        $query = Banner::query();
        $user = $request->user();

        if ($user->type == 4) {
            $query->where('org_id', '=', 0)->where('platform_id', '=', 0);
        } else if ($user->type == 3) {
            $query->where('org_id', '=', $user->org_id);
        } else if ($user->type == 5) {
            $query = $query->where('platform_id', '=', $user->platform_id);
        }

        $info = $query->paginate($perPage)->toArray();

        return $this->returnJSON([
            'count' => $info['total'],
            'rows' => $info['data']
        ]);
    }

    public function store(Request $request)
    {
        $this->validate($request, [
            'photo' => 'required|max:255',
            'href' => 'max:1024',
        ]);

        $node = new Banner();
        $data = $request->only([
            'title',
            'photo',
            'href',
            'jump_mode',
            'jump_id'
        ]);

        $data['status'] = 1;
        $data['jump_mode'] = intval($data['jump_mode']) || 0;
        $data['jump_id'] = intval($data['jump_id']) || 0;
        $data['sort'] = 0;
        $data['org_id'] = $request->user()->org_id;
        $data['start'] = date('Y-m-d');
        $data['end'] = date('Y-m-d');

        if($request->user()->type == 5)
        {
            $data['platform_id'] = $request->user()->platform_id;
        }

        $node->fill($data);
        if ($node->save()) {
            return $this->returnJSON(null, 0, '新增成功');
        }
        return $this->returnJSON(null, 500, '新建失败');

//        $banners = $request->get('banner');
//
//        $orgId = $request->user()->org_id;
//
//        if (is_array($banners)) {
//            Banner::where('org_id', '=', 0)->delete();
//            $inserts = array_map(function($item) use($orgId) {
//                if (!isset($item['title'])) {
//                    $item['title'] = '';
//                }
//
//                if (!isset($item['jump_mode'])) {
//                    $item['jump_mode'] = 0;
//                }
//
//                if (!isset($item['sort'])) {
//                    $item['sort'] = 0;
//                }
//
//                if (!isset($item['start'])) {
//                    $item['start'] = date('Y-m-d');
//                }
//
//                if (!isset($item['end'])) {
//                    $item['end'] = date('Y-m-d');
//                }
//
//                $item['org_id'] = $orgId;
//                unset($item['updated_at']);
//                unset($item['created_at']);
//
//                return $item;
//            }, $banners);
//            Banner::insert($inserts);
//        }
//
//        return $this->index($request);
    }

    public function update(Request $request, $id) {
        $node = Banner::findOrFail($id);

        $this->validate($request, [
            'photo' => 'required|max:255',
            'href' => 'max:1024',
        ]);

        $data = $request->only([
            'photo',
            'href',
            'jump_mode'
        ]);

        $node->fill($data);
        if ($node->save()) {
            return $this->returnJSON(null, 0, '保存成功');
        }
        return $this->returnJSON(null, 500, '保存失败');
    }

    public function destroy(Request $request, $id)
    {
        $ids = array_map(function($item) {
            return intval($item);
        }, explode(",", $id));

        if (Banner::whereIn('id', $ids)->delete()) {
            return $this->returnJSON($ids, 0, '删除成功');
        } else {
            return $this->returnJSON(null, 1, "删除失败");
        }
    }

    public function online(Request $request, $id)
    {
        $ids = array_map(function($item) {
            return intval($item);
        }, explode(",", $id));
        $result = Banner::whereIn('id',  $ids)->update(['status' => 1]);
//    Log::info(Post::where('id', $ids)->toSql());
        if ($result) {
            return $this->returnJSON($ids, 0, '启用成功');
        } else {
            return $this->returnJSON(null, 1, "启用失败");
        }
    }
    public function offline(Request $request, $id)
    {
        $ids = array_map(function($item) {
            return intval($item);
        }, explode(",", $id));
        $result = Banner::whereIn('id',  $ids)->update(['status' => 0]);
//    Log::info(Post::where('id', $ids)->toSql());
        if ($result) {
            return $this->returnJSON($ids, 0, '禁用成功');
        } else {
            return $this->returnJSON(null, 1, "禁用失败");
        }
    }
}
